package spiderlog

import (
	"fmt"
	"log"
	"math/rand"
	"os"
	"time"
)

func init()  {
	logPath:="./log/"
	_,err:=os.Stat(logPath)
	if err!=nil{
		if !os.IsExist(err){
			err=os.MkdirAll(logPath,0644)
			if err!=nil{
				panic(err)
			}
		}

	}

	file := logPath +logDate()
	logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err != nil {
		panic(err)
	}
	log.SetOutput(logFile) // 将文件设置为log输出的文件
	log.SetPrefix("["+RandString(6)+"] ")
	log.SetFlags(log.LstdFlags | log.Lshortfile )
}

func logDate()(string)  {
	now := time.Now()

	year := now.Year()
	month := now.Month()
	day := now.Day()

	// %02d表示不够2位的话，就补0
	return fmt.Sprintf("%02d-%02d-%02d ", year, month, day)
}
func RandString(len int) string {
	r:= rand.New(rand.NewSource(time.Now().Unix()))
	bytes := make([]byte, len)
	for i := 0; i < len; i++ {
		b := r.Intn(26) + 65
		bytes[i] = byte(b)
	}
	return string(bytes)
}

